<?php


namespace app\operates\controller;


use think\facade\Db;

class Timer
{
    /**
     * @autho:xiangzi
     * @auth:true
     * @Date:2025/3/24
     * 一天调一次的定时器入口
     */
    public function dayTimer()
    {
        $this->htQdTipTimer();
        $this->lyTipTimer();
        return 1;
    }
    /**
     * @autho:xiangzi
     * @auth:true
     * @Date:2025/3/24
     * 合同签订时间提示
     */
    public function htQdTipTimer()
    {
        $days=(int)dataConfig('contact_begain_befor');
        $date=date("Y-m-d", strtotime("+{$days} days"));
     $where[]=['status','>',-1];
//     $where[]=['dataType','=',1];
     $where[]=['htglStatus','=','3'];
     $where[]=['jbId','>','0'];
     $where[]=['htQdTime','=',$date];
     $list=Db::name('procurement')->where($where)
             ->order('htQdTime asc')
         ->field('id,name,code,jbId,htQdTime')
             ->select()->toArray()??[];
     if (empty($list)){
         echo '合同提示无';
         return 1;
     }
        try {
            foreach ($list as $v){
                $title='项目:'.$v['name'].'   编号:'.$v['code'].' 签订时间于'.$v['htQdTime'].'到期';
                $msg=[
                    'from_uid'=>0,//发送人
                    'to_uids'=>$v['jbId'],//接收人
                    'template_id'=>'note',//消息模板ID
                    'content'=>[ //消息内容
                        'create_time'=>date('Y-m-d H:i:s'),
                        'title' => $title,
                        'action_id'=>$v['id']
                    ]
                ];
                send_message($msg,$title);
            }
        }catch (\Exception $e){
         echo '合同错误：'.$e->getMessage();
        }
     return 1;

}

    /**
     * @return int
     * @autho:xiangzi
     * @auth:true
     * @Date:2025/3/24
     * 合同履约时间
     */
    public function lyTipTimer()
    {
        $days=(int)dataConfig('contact_performance_befor');
        $date=date("Y-m-d", strtotime("+{$days} days"));
        $where[]=['status','>',-1];
//        $where[]=['dataType','=',1];
        $where[]=['htglStatus','=','3'];
        $where[]=['jbId','>','0'];
        $where[]=['htLyEnd','=',$date];
        $list=Db::name('procurement')->where($where)
                ->order('htLyBegain asc')
                ->field('id,name,code,jbId,htLyEnd')
                ->select()->toArray()??[];
        if (empty($list)){
            echo '履约提示无';
            return 1;
        }
        try {
            foreach ($list as $v){
                $title='项目:'.$v['name'].'   编号:'.$v['code'].' 履约时间于'.$v['htLyEnd'].'截止';
                $msg=[
                    'from_uid'=>0,//发送人
                    'to_uids'=>$v['jbId'],//接收人
                    'template_id'=>'note',//消息模板ID
                    'content'=>[ //消息内容
                        'create_time'=>date('Y-m-d H:i:s'),
                        'title' => $title,
                        'action_id'=>$v['id']
                    ]
                ];
                send_message($msg,$title);
            }
        }catch (\Exception $e){
            echo '履约错误：'.$e->getMessage();
        }
        return 1;
}
}